package com.rs.game;

import java.io.Serializable;

import com.rs.Settings;
import com.rs.utils.Utils;

public class WorldTile implements Serializable {

	private static final long serialVersionUID = -6567346497259686765L;
	
	private short x, y;
	private byte plane;
	
	public WorldTile(int x, int y, int plane) {
		this.x = (short) x;
		this.y = (short) y;
		this.plane = (byte) plane;
	}
	
	public WorldTile(WorldTile tile) {
		this.x = tile.x;
		this.y = tile.y;
		this.plane = tile.plane;
	}
	
	public WorldTile(WorldTile tile, int randomize) {
		this.x = (short) (tile.x+Utils.getRandom(randomize*2)-randomize);
		this.y = (short) (tile.y+Utils.getRandom(randomize*2)-randomize);
		this.plane = tile.plane;
	}
	
	public void moveLocation(int xOffset, int yOffset, int planeOffset) {
		x += xOffset;
		y += yOffset;
		plane += planeOffset;
	}
	
	public final void setLocation(WorldTile tile) {
		setLocation(tile.x, tile.y, tile.plane);
	}
	
	public final void setLocation(int x, int y, int plane) {
		this.x = (short) x;
		this.y = (short) y;
		this.plane = (byte) plane;
	}
	
	public int getX() {
		return x;
	}
	
	public int getY() {
		return y;
	}
	
	public int getPlane() {
		return plane;
	}
	
	public int getRegionX() {
		return (x >> 3);
	}
	
	public int getRegionY() {
		return (y >> 3);
	}
	
	public int getRegionId() {
		return (((getRegionX() / 8) << 8) + (getRegionY() / 8));
	}
	
	public int getLocalX(WorldTile tile, int mapSize) {
		return x - 8 * (tile.getRegionX() - (Settings.MAP_SIZES[mapSize] >> 4));
	}
	
	public int getLocalY(WorldTile tile, int mapSize) {
		return y - 8 * (tile.getRegionY() - (Settings.MAP_SIZES[mapSize] >> 4));
	}
	
	
	public int getLocalX(WorldTile tile) {
		return getLocalX(tile, 0);
	}
	
	public int getLocalY(WorldTile tile) {
		return getLocalY(tile, 0);
	}
	
	public int getLocalX() {
		return getLocalX(this);
	}
	
	public int getLocalY() {
		return getLocalY(this);
	}
	
    public boolean withinDistance(WorldTile tile, int distance) {
        if (tile.plane != plane)
            return false;
        int deltaX = tile.x - x, deltaY = tile.y - y;
        return deltaX <= distance && deltaX >= -distance && deltaY <= distance && deltaY >= -distance;
    }
	
    public boolean withinDistance(WorldTile tile) {
        if (tile.plane != plane)
            return false;
        int deltaX = tile.x - x, deltaY = tile.y - y;
        return deltaX <= 14 && deltaX >= -15 && deltaY <= 14 && deltaY >= -15;
    }
    
    
    public int getCoordFaceX(int sizeX) {
    	return getCoordFaceX(sizeX, -1, -1);
    }
    
    public static final int getCoordFaceX(int x, int sizeX, int sizeY, int rotation) {
    	return x + ((rotation == 1 || rotation == 3 ? sizeY : sizeX) -1)/2;
    }
    
    public static final int  getCoordFaceY(int y, int sizeX, int sizeY, int rotation) {
    	return y + ((rotation == 1 || rotation == 3 ? sizeX : sizeY) -1)/2;
    }
    
    public int getCoordFaceX(int sizeX, int sizeY, int rotation) {
    	return x + ((rotation == 1 || rotation == 3 ? sizeY : sizeX) -1)/2;
    }
    
    public int getCoordFaceY(int sizeY) {
    	return getCoordFaceY(-1, sizeY, -1);
    }
    
    public int getCoordFaceY(int sizeX, int sizeY, int rotation) {
    	return y + ((rotation == 1 || rotation == 3 ? sizeX : sizeY) -1)/2;
    }
    
}
